﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Common.Library;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Collections.ObjectModel;
using OnlineVideos.UI;

namespace Social_Celebrities.Views
{
    public partial class OnlineImages : PhoneApplicationPage
    {     
        ObservableCollection<ImageSource> source = new ObservableCollection<ImageSource>();
        int count1 = 0;
        int count = 0;
        public OnlineImages()
        {
            InitializeComponent();
            Loaded += OnlineImages_Loaded;
        }
       
        void OnlineImages_Loaded(object sender, RoutedEventArgs e)
        {
            if (NetworkHelper.IsNetworkAvailableForDownloads())
            {
                _PerformanceProgressBar.IsIndeterminate = true;
                if (Constants.OnlineImageUrls.Count > 0)
                {
                    lbxImages.ItemsSource = source;
                    foreach (string url in Constants.OnlineImageUrls.Where(i => i != null))
                    {
                        BitmapImage bitmap = new BitmapImage();
                        bitmap.CreateOptions = BitmapCreateOptions.None;
                        bitmap.UriSource = new Uri(url, UriKind.RelativeOrAbsolute);
                        bitmap.ImageOpened += bitmap_ImageOpened;
                        bitmap.ImageFailed += bitmap_ImageFailed;
                    }
                    notavailablemes.Visibility = Visibility.Collapsed;
                }
                else
                {
                    notavailablemes.Visibility = Visibility.Visible;
                }
            }
            else
                //MessageBox.Show("No Network Available..!!");
                NoNetworkMsg.Visibility = Visibility.Visible;
            
        }

        void bitmap_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

            if (++count == Constants.OnlineImageUrls.Count)
            {
                _PerformanceProgressBar.IsIndeterminate = false;
            }           
        }

        void bitmap_ImageOpened(object sender, RoutedEventArgs e)
        {
            source.Add((sender as BitmapImage) as ImageSource);

            if (++count == Constants.OnlineImageUrls.Count)
            {
                _PerformanceProgressBar.IsIndeterminate = false;
            }
        }

        private void imgTitle_MouseEnter(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }      

        private void lbxImages_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            Constants.UserImage_socialcelebs = new BitmapImage();
            Constants.UserImage_socialcelebs.UriSource = new Uri(((BitmapImage)(sender as ListBox).SelectedItem).UriSource.ToString(), UriKind.RelativeOrAbsolute);
            NavigationService.Navigate(new Uri("/Views/PhotoChooser.Xaml" , UriKind.Relative));
        }       
    }
}